#pragma once
#include<iostream>
#include<vector>
#include<string>
#include<sstream>
#include<unordered_map>
const static std::string base_sep="\r\n";
const static std::string line_sep = ": ";
class HttpRequest
{
private:
    std::string GetLine(std::string &reqstr)
    {
        auto pos=reqstr.find(base_sep);
        if(pos==std::string::npos)
        {
            return std::string ();
        }
        std::string line =reqstr.substr(0,pos);
        reqstr.erase(0,line.size()+base_sep.size());
        return line.empty()?base_sep:line;
    }
    void ParseReqLine()
    {
        std::stringstream ss(_req_line);//这列就想cin 
        ss>>_method>>_url>>_version;//这里使用空格分隔这就是就是将流输入字符串

    }
    void ParseReqHeader()
    {
        for(auto &header :_req_headers)
        {
            auto pos=header.find(line_sep);
            if(pos==std::string ::npos)
             continue;
            std::string k=header.substr(0,pos);
            std::string v=header.substr(pos+line_sep.size());
            if(k.empty()||v.empty()) continue;
            _header_kv.insert(std::make_pair(k,v));
        }
    }
public:
    HttpRequest():_blank_line(base_sep)
    {
        
    }
    void Deserialize(std::string &reqstr)
    {
        _req_line=GetLine(reqstr);//请求行
        std::string header;//请求报头
        do
        {
            header=GetLine(reqstr);
            if(header.empty()) break;//这里就是为删除空行的
            else if(header ==base_sep) break;
            _req_headers.push_back(header);
        } while (true); 
        if(reqstr.empty())
        {
            _body_text=reqstr;
        }
        ParseReqLine();
        ParseReqHeader();

    }  
    void Print()
    {
        std::cout<<"--------------------------"<<std::endl;
        std::cout<<"###"<<_req_line<<std::endl;
        for(auto &header :_req_headers)
        {
            std::cout<<"@@@"<<header<<std::endl;
        }
        std::cout<<"***"<<_blank_line;
        std::cout<<">>>"<<_body_text<<std::endl;

        std::cout << "Method: " << _method << std::endl;
        std::cout << "Url: " << _url << std::endl;
        std::cout << "Version: " << _version << std::endl;
        for(auto &header_kv:_header_kv)
        {
            std::cout<<")))"<<header_kv.first<<"->"<<header_kv.second<<std::endl;
        }

    } 
    ~HttpRequest()
    {
 
    }

private:
    std::string _req_line;
    std::vector<std::string>_req_headers;
    std::string _blank_line;
    std::string _body_text;


    std::string _method;
    std::string _url;
    std::string _version;
    std::unordered_map<std::string ,std::string>_header_kv;
};
class HttpServer
{
    public:
        HttpServer()
        {}
        std::string HandlerHttpRequest(std::string &reqstr)
        {
#ifdef TEST //如果前面使用宏定义TEST就执行这里的内容
            std::cout<<"----------------------------------------"<<std::endl;
            std::cout<<req;
            std::string respondsestr="HTTP/1.1 200 OK\r\n";
            respondsestr+="Content-Type: text/html\r\n";
            respondsestr+="\r\n";
            respondsestr+="<html><h1>hello Linux, hello bite !</h1><html>";

            return respondsestr;
#else  //如果上面的没有定义宏就执行这里的代码从else到endif
        HttpRequest req;
        req.Deserialize(reqstr);
        req.Print();
        return std::string();
#endif
        }
        ~HttpServer()
        {

        }
};